import {request} from '../../network/index'
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isPlay:false,
		song:'',
		currentTime:0,
		durationTime:0,
		value:1,
		like:false,
		show:false,
		lyric:[]
	},
	async getLyric(){
		let res = await request('/lyric',{id:this.data.song.id}) 
		let lyric = res.data.lrc.lyric.split(/\n/igs).map(item => {
			let min = item.slice(1,3)
			let sec = item.slice(4,6)
			let mini = item.slice(7,10)
			return {
				min,sec,mini,
				lyric:item.slice(10,item.length).replace(/]/g,''),
				item,
				time:parseInt(mini) + parseInt(sec) * 1000 + parseInt(min) *60 *1000
			}
		})
	
		this.setData({
			show:true,
			lyric
		})
		console.log(lyric);
	},
	onClose(){
		this.setData({
			show:false
		})
	},
	//收藏音乐
	async isLike(){
		this.setData({
			like:!this.data.like
		})
		let link = await request('/like',{id:this.data.song.id,link:this.data.like})
		console.log(link);
	},
	//音乐播放暂停
	play(event){
		this.setData({
			isPlay:!this.data.isPlay
		})
		if(this.data.isPlay){
			this.music.src = `https://music.163.com/song/media/outer/url?id=${this.data.song.id}.mp3`
			this.music.title = this.data.song.name
		}else{
			this.music.pause()
		}
	},
	//该变播放进度
	swipe(){
		let changeValue = (this.data.value / 100) * (this.data.durationTime / 1000)
		this.music.seek(changeValue)
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	 onLoad: async function (options) {
	
	 //创建音乐管理器实例
	 this.music = wx.getBackgroundAudioManager()

		let songDetail = await request('/song/detail',{ids:options.id})
		console.log(songDetail);
		this.setData({
			song:songDetail.data.songs[0]
		})
				//喜欢音乐
			 let userinfo = wx.getStorageSync('profile')
			 request('/likelist',{uid:userinfo.userId}).then(res=>{
					this.setData({
						like:res.data.ids.every(item=>item.id === this.data.song.id)
					})
			 })
		 //音乐是否在播放
		 if(app.globalData.isMusicPlay&&app.globalData.musicId===this.data.song.id){
			this.setData({
				isPlay:true
			})
		 }
		//监听音乐播放进度
		this.music.onTimeUpdate(()=>{
			console.log(this.music.currentTime * 1000);
			this.setData({
				currentTime:this.music.currentTime * 1000,
				durationTime:this.music.duration * 1000,
				value:this.music.currentTime/this.music.duration * 100
			})
		})
		//监听系统音乐开关栏
		this.music.onPlay(()=>{
			app.globalData.isMusicPlay = true
			app.globalData.musicId = this.data.song.id
			this.setData({
				isPlay:true
			})
		})
		this.music.onPause(()=>{
			app.globalData.isMusicPlay = false
			this.setData({
				isPlay:false
			})
		})
		this.music.onStop(()=>{
			app.globalData.isMusicPlay = false
			this.setData({
				isPlay:false
			})
		})
		//监听音乐自然结束
		this.music.onEnded(() =>{
			console.log('音乐结束了');
			this.getOpenerEventChannel().emit('change',{change:'1'})

			let musicData = wx.getStorageSync('musicData')
			this.setData({
				song:musicData
			})
				this.music.src = `https://music.163.com/song/media/outer/url?id=${this.data.song.id}.mp3`
				this.music.title = this.data.song.name
			})
	},
	//音乐切换
	change(event){
		let params = event.currentTarget.dataset.params
		this.getOpenerEventChannel().emit('change',{change:params})

		let musicData = wx.getStorageSync('musicData')
		this.setData({
			song:musicData
		})

		if(this.data.isPlay){
			this.music.src = `https://music.163.com/song/media/outer/url?id=${this.data.song.id}.mp3`
			this.music.title = this.data.song.name
		}else{
			this.music.pause()
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		
	},	

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})